<template>
    <div class="blog-header">
        <el-drawer :visible.sync="drawer" direction="ltr">
            <el-row class="tac" style="width: 100%; margin: 0;">
                <el-col :span="24">
                    <el-menu :default-active="headerIndex" class="el-menu-vertical-demo" active-text-color="#a31f34"
                        style="text-align: center;">
                        <el-menu-item index="1">
                            <nuxt-link class="nuxtLink" to="/">首页</nuxt-link>
                        </el-menu-item>
                        <el-menu-item index="3">
                            <nuxt-link class="nuxtLink" to="/article/info" exact>消息中心</nuxt-link>
                        </el-menu-item>
                        <el-menu-item index="4">
                            <nuxt-link class="nuxtLink" to="/article/add">文章发布</nuxt-link>
                        </el-menu-item>
                        <el-menu-item index="2">
                            <nuxt-link class="nuxtLink" to="/">关于作者</nuxt-link>
                        </el-menu-item>
                    </el-menu>
                </el-col>
            </el-row>
        </el-drawer>
        <el-row :gutter="10">
            <!-- 大于 992 隐藏 -->
            <el-col class="hidden-md-and-up" :xs="4" :sm="4" :md="2">
                <div class="head-left">
                    <div class="el-icon-s-operation blog-menu" @click="drawer = true"></div>
                    <!-- <el-button @click="drawer = true" type="primary" style="margin-left: 16px;" icon="el-icon-menu"></el-button> -->
                </div>

            </el-col>
            <el-col :xs="16" :sm="16" :md="4" :lg="4">
                <div class="head-left">绫三博客</div>
            </el-col>
            <el-col :xs="2" :sm="2" :md="14" :lg="12">
                <!-- 小于992隐藏 -->
                <el-menu :default-active="headerIndex" class="el-menu-demo hidden-sm-and-down" mode="horizontal"
                    @select="handleSelect" text-color="#fff" background-color="transparent" active-text-color="#a31f34">
                    <el-menu-item index="articleIndex">
                        <router-link class="nuxtLink" to="/article/index">首页</router-link>
                        <!-- <span class="nuxtLink" @click="$emit('checkComponents', 'articleIndex')">首页</span> -->
                    </el-menu-item>
                    <el-menu-item index="msg">
                        <nuxt-link class="nuxtLink" to="/chat/msg">消息中心</nuxt-link>
                        <!-- <span class="nuxtLink" @click="$emit('checkComponents', 'msg')">消息中心</span> -->
                    </el-menu-item>
                    <el-menu-item index="articleAdd">
                        <nuxt-link class="nuxtLink" to="/article/add">文章发布</nuxt-link>
                        <!-- <span class="nuxtLink" @click="$emit('checkComponents', 'articleAdd')">文章发布</span> -->
                    </el-menu-item>
                    <!-- <el-menu-item index="articleIndex">
                        <span class="nuxtLink" @click="$emit('checkComponents', 'articleIndex')">关于作者</span>
                    </el-menu-item> -->
                </el-menu>
            </el-col>
            <el-col :xs="4" :sm="4" :md="2" :lg="4">
                <!-- 小于1200隐藏 -->
                <div style="margin-top: 10px;" class="hidden-md-and-down">
                    <!-- <el-input placeholder="说一说你想搜的东西吧！" v-model="input3" class="input-with-select">
                        <el-button slot="append" icon="el-icon-search"></el-button>
                    </el-input> -->
                    <div class="input-container">
                        <input type="text" v-model="searchContent" name="text" class="input" placeholder="说一说你想搜的东西吧！" autocomplete="off" @keyup.enter="search">
                        <span class="icon" @click="search()">
                            <svg width="19px" height="19px" viewBox="0 0 24 24" fill="none"
                                xmlns="http://www.w3.org/2000/svg">
                                <g id="SVGRepo_bgCarrier" stroke-width="0"></g>
                                <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
                                <g id="SVGRepo_iconCarrier">
                                    <path opacity="1" d="M14 5H20" stroke="#000" stroke-width="1.5"
                                        stroke-linecap="round" stroke-linejoin="round"></path>
                                    <path opacity="1" d="M14 8H17" stroke="#000" stroke-width="1.5"
                                        stroke-linecap="round" stroke-linejoin="round"></path>
                                    <path
                                        d="M21 11.5C21 16.75 16.75 21 11.5 21C6.25 21 2 16.75 2 11.5C2 6.25 6.25 2 11.5 2"
                                        stroke="#000" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
                                    </path>
                                    <path opacity="1" d="M22 22L20 20" stroke="#000" stroke-width="3.5"
                                        stroke-linecap="round" stroke-linejoin="round"></path>
                                </g>
                            </svg>
                        </span>
                    </div>
                </div>
                <!-- 大于1200隐藏 -->
                <div class="hidden-lg-and-up search-button" @click="expandDialog" ref="searchButton">
                    <!-- <el-button slot="append" icon="el-icon-search" class=""></el-button> -->
                    <div class="el-icon-search" style="color: black; font-size: 1.5em; 
                    font-weight: bolder; margin-top: 10px;"></div>
                </div>
            </el-col>
            <transition name="el-zoom-in-center">
                <div v-show="dialogExpanded" id="blog-dialog" ref="blogDialog">
                    <div class="dialog-text">搜索</div>
                    <el-input v-model="searchContent" placeholder="说一说你想搜的东西吧！"></el-input>
                </div>
            </transition>

            <!-- 小于992隐藏 -->
            <el-col :xs="1" :sm="1" :md="4" :lg="4" class="hidden-sm-and-down">
                <div class="head-right" v-if="!token">
                    <button ref="loginButton" class="button" @click="updateLoginIsShow"> 登入 <span
                            style="color: black;">|</span> 注册 </button>
                </div>

                <div style="margin: 0 40%;" v-else>
                    <el-dropdown placement="bottom" trigger="hover">
                        <el-image style="width: 50px; height: 50px; margin-top: 5px;" :src="avatar" fit="fill">
                        </el-image>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="toUser()">个人中心</el-dropdown-item>
                            <!-- <el-dropdown-item>我的文章</el-dropdown-item>
                            <el-dropdown-item>浏览历史</el-dropdown-item> -->
                            <el-dropdown-item @click.native="logout()">退出登入</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </div>
            </el-col>
            <!-- 登入对话框 -->
            <BlogLogin :loginButton="this.$refs.loginButton" ref="blogLogin" />
        </el-row>
    </div>
</template>

<script>
import BlogLogin from '~/pages/layouts/BlogLogin.vue'
import { mapGetters } from 'vuex'

export default {
    components: { BlogLogin },
    data() {
        return {
            drawer: false,
            dialogExpanded: false,
            searchContent: '',
            dialogFormVisible: false,
            avatarFlag: false,
            userInfo: {},
        };
    },
    props: ['headerIndex'],

    computed: {
        ...mapGetters([
            'token', 'avatar'
        ])
    },
    methods: {
        toUser(){
            this.$router.push("/user")
        },
        search(){
            this.$bus.$emit("getArticleList", {digest: this.searchContent})
        },
        async logout() {
            await this.$store.dispatch('user/logout').then(res => {
                window.location.reload()
            })
        },
        updateLoginIsShow() {
            this.$store.dispatch('user/updateLoginStatus', true)
            // this.loginIsShow = true
            document.body.style.overflow = "hidden";
        },
        expandDialog() {
            this.dialogExpanded = true;
        },
        handleSelect(key, keyPath) {
            this.$router.push({ path: "/", query: { key: key } })
        },
        // 自定义对话框，关闭函数
        closeDialog(event) {
            if (this.dialogExpanded) {
                const target = event.target;
                const searchButton = this.$refs.searchButton;
                const blogDialog = this.$refs.blogDialog;
                // 当点击位置不是搜索图标和对话框时，才会触发
                if (target !== searchButton && !searchButton.contains(target) &&
                    target !== blogDialog && !blogDialog.contains(target)) {
                    // 关闭对话框
                    this.dialogExpanded = false;
                }
            }
        },
    },
    mounted() {
        document.addEventListener('click', this.closeDialog);
    },
    beforeDestroy() {
        document.removeEventListener('click', this.closeDialog);
    },
}
</script>

<style scoped>
.nuxtLink {
    display: inline-block;
    padding: 0;
    width: 100%;
    height: 100%;
}

::v-deep .el-image__inner {
    width: 100%;
    height: 100%;
    border-radius: 50%;
}


.btn {
    transition: all 0.3s ease-in-out;
    font-family: "Dosis", sans-serif;
}

.btn {
    width: 150px;
    /* height: 60px; */
    border-radius: 50px;
    background-image: linear-gradient(135deg, #feb692 0%, #ea5455 100%);
    box-shadow: 0 20px 30px -6px rgba(238, 103, 97, 0.5);
    outline: none;
    cursor: pointer;
    border: none;
    font-size: 24px;
    color: white;
}

.btn:hover {
    transform: translateY(3px);
    box-shadow: none;
}

.btn:active {
    opacity: 0.5;
}

.input-container {
    /* width: 220px; */
    position: relative;
}

.icon {
    position: absolute;
    right: 10px;
    top: calc(50% + 5px);
    transform: translateY(calc(-50% - 5px));
}

.input {
    width: 100%;
    height: 40px;
    padding: 10px;
    transition: .2s linear;
    border: 2.5px solid black;
    font-size: 14px;
    /* text-transform: uppercase; */
    letter-spacing: 2px;
    background-color: transparent;
    font-family: cursive;
    border-radius: 10px;
    color: white;
}

.input::placeholder {
    color: #e8e8e8;
    font-size: 12px;
}

.input:focus {
    outline: none;
    border: 0.5px solid black;
    box-shadow: -5px -5px 0px black;
}

.input-container:hover>.icon {
    animation: anim 1s linear infinite;
}

@keyframes anim {

    0%,
    100% {
        transform: translateY(calc(-50% - 5px)) scale(1);
    }

    50% {
        transform: translateY(calc(-50% - 5px)) scale(1.1);
    }
}

.dialog-text {
    margin: 25px;
    color: #cbcbd1;
    font-size: x-large;
    font-family: cursive;
}

#blog-dialog {
    position: absolute;
    top: 100px;
    left: 15%;
    width: 70%;
    height: 150px;
    /* border: 1px red solid; */
    border-radius: 3px;
    border-radius: 10px !important;
    background-color: #1b1e24 !important;
    z-index: 2200
}

#blog-dialog .el-input {
    margin: 0 5%;
    width: 90% !important;
    border-radius: 30px;
    border: none;
}


::v-deep .el-drawer.ltr {
    height: 100vh;

}

::v-deep .el-drawer {
    width: 250px !important;
}

::v-deep .el-drawer__wrapper {
    position: absolute;
    height: 100vh !important;
}

.blog-header {
    padding: 0;
    height: 100%;
    background: linear-gradient(120deg, rgba(247, 135, 54, 0.208) 0%, rgba(54, 125, 247, 0.208) 100%);
    box-shadow: rgba(255, 255, 255, 0.05) 0px 6px 24px 0px, rgba(255, 255, 255, 0.08) 0px 0px 0px 1px;
}

.header-back {
    background-color: #1b1e24;
}

::v-deep .el-row {
    margin: 0 !important;
}

.head-left,
.head-right {
    line-height: 55px;
    margin: 0 auto;
    text-align: center;
    font-family: cursive !important;
    font-size: x-large !important;
    font-weight: bold !important;
    position: relative;
}


.button {
    --color: white;
    padding: 0.8em 1.7em;
    background-color: transparent;
    border-radius: .3em;
    position: relative;
    overflow: hidden;
    cursor: pointer;
    transition: .5s;
    font-weight: 400;
    font-size: 14px;
    border: 2px solid black;
    font-family: inherit;
    text-transform: uppercase;
    color: var(--color);
    z-index: 1;
}

.button::before,
.button::after {
    content: '';
    display: block;
    width: 50px;
    height: 50px;
    transform: translate(-50%, -50%);
    position: absolute;
    border-radius: 50%;
    z-index: -1;
    background-color: var(--color);
    transition: 1s ease;
}

.button::before {
    top: -2em;
    left: -1em;
}

.button::after {
    left: calc(100% + 2em);
    top: calc(100% + 1em);
}

.button:hover::before,
.button:hover::after {
    height: 410px;
    width: 410px;
}

.button:hover {
    color: rgb(10, 25, 30);
}

.button:active {
    filter: brightness(.8);
}


.search-button {
    margin-top: 10px !important;
    text-align: right;
    margin-right: 10px;
}

.el-col {
    border-radius: 4px;
}

.el-menu-demo {
    border-bottom: none !important;
}

.blog-menu {
    margin-right: 100px;
    font-size: 1.5em;
}


::v-deep .el-menu-demo .el-menu-item a {
    font-family: cursive !important;
    font-size: medium !important;
    color: #d2d2d7 !important;
    text-decoration: none;
}

::v-deep .el-menu-demo .el-menu-item:hover {
    background-color: transparent !important;
}

::v-deep .el-menu-demo .el-menu-item a:hover {
    color: #a31f34 !important;
}

::v-deep .tac a {
    text-decoration: none;
    color: black;
}
</style>
